/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.data.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.pay.order.data.dao.PayinOrderDao;
import com.yyfax.pay.order.model.po.PayinBatchOrderPO;
import com.yyfax.pay.order.model.po.PayinOrderPO;
import com.yyfax.pay.order.model.po.PayoutOrderPO;

/**
 * 批量代付订单持久化实现
 *
 * @author maojian
 * @date 2017年04月12日 11:09:15
 * @since 1.0.0
 */
@Repository
public class PayinOrderDaoImpl extends BaseDaoImpl<PayinOrderPO, Integer> implements PayinOrderDao {
	
	private static final String TABLE_NAME = "payin_order";

	private final String SQL_INSERT = "insert";
	
	private final String SQL_SELECT_BY_ID = "selectById";
	
	private final String SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE = "selectByOrderIdAndMerchantCode";
	
	private final String SQL_SELECT_BY_MERCHANTCODE_AND_PAYINBATCHORDERID = "selectByMerchantCodeAndPayinBatchOrderId";

	public PayinOrderDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insertReturnPK(PayinOrderPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.insertForPK(sql, sqlParameterSource);
	}

	@Override
	public PayinOrderPO selectByOrderIdAndMerchantCode(String orderId, String merchantCode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("orderId", orderId);
		params.addValue("merchantCode", merchantCode);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PayinOrderPO>(poClass));
	}

	@Override
	public List<PayinOrderPO> selectByMerchantCodeAndPayinBatchOrderId(
			String merchantCode, String payinBatchOrderId) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCHANTCODE_AND_PAYINBATCHORDERID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("payinBatchOrderId", payinBatchOrderId);
		return namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<PayinOrderPO>(poClass));
	}

	

}
